package managedbean;

import entity.Employee;
import javaclass.RedirectProperties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpSession;
import session.stateless.EmployeeBeanLocal;
import session.stateless.MessageMeSessionLocal;

@ManagedBean(name="controller") 
@SessionScoped
public class Controller {
    private String message; 
    private String userName; 
    private String password;
    private Employee employee;
    private String statusMessage;
    
    @EJB
    private EmployeeBeanLocal employeeSessionBeanLocal;
    @EJB
    private MessageMeSessionLocal messageMeSessionLocal;
    
    public String getMessage() { 
        return message; 
    } 


    public void setMessage(String message) { 
        this.message = message; 
    } 


    public String getUserName () { 
        return userName; 
    } 


    public void setUserName (String username) { 
        this.userName = username; 
    } 


    public String getPassword () { 
        return password; 
    } 


    public void setPassword (String password) { 
        this.password = password; 
    }

    public Employee getEmployee() {
        return employee;
    }

    public void setEmployee(Employee employee) {
        this.employee = employee;
    }

    public void login(){
        
        String temp_UserName = String.valueOf(userName);
        String temp_Password = String.valueOf(password);
        
            try { 
                
                employee = employeeSessionBeanLocal.find(userName, password);
                
                //if correct login and user is activated and it is not first login
                if ((employee != null) && (employee.getActivatedStatus()) && (employee.getAttempt() != -1)){
                    this.userName = this.password = null;
                    ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
                    RedirectProperties rp = new RedirectProperties();
                    ec.redirect(rp.getUrl() + "/innersystem/index.xhtml");
                } 
                
                //if correct login and user is activated and it is first login
                if ((employee != null) && (employee.getActivatedStatus()) && (employee.getAttempt() == -1)){
                    this.userName = this.password = null;
                    ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
                    RedirectProperties rp = new RedirectProperties();
                    ec.redirect(rp.getUrl() + "/innersystem/resetPassword.xhtml");
                }
                
                //if correct login and user is decactivated
                
                if (employee != null && !employee.getActivatedStatus()){
                    
                    statusMessage = "\n\n\nYour account is deactivated.\nPlease contact adminstrator for more information.!"; 
                    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, this.statusMessage, "")); 
                }
                
                //if wrong login
                if (employee == null){
                    Employee employeeTest = employeeSessionBeanLocal.getEmployee(userName);
                    //check whether username is correct
                    if (employeeTest != null){
                        employeeSessionBeanLocal.updateWrongLogin(userName);
                    }

                    statusMessage = "\n\n\nWrong username or password.\nPlease try again!"; 
                    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, this.statusMessage, ""));                    
                    employeeTest = null;
                }                
            } 
            catch(Exception ex) 
            { 
                ex.printStackTrace(); 
            } 
        
    }
    
    public void logOut() throws ServletException {
        this.employee = null;
        
        ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
            try {
                //kill the session
                ((HttpSession)ec.getSession(true)).invalidate();
                RedirectProperties rp = new RedirectProperties();
                ec.redirect(rp.getUrl() + "/index.xhtml");
            } catch (Exception ex) {
                Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ex);
            }
        

//        try { 
//            //FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove("username");
//            FacesContext.getCurrentInstance().getExternalContext().redirect("index.xhtml");
//        }
//                    catch(Exception ex) 
//            { 
//                ex.printStackTrace(); 
//            } 
    }
    
    public boolean isLoggedIn(){
        return (employee != null);
    }
    
    public boolean isNotLoggedIn(){
        return (employee == null);
    }
    
    public boolean isAdmin(){
        return employeeSessionBeanLocal.isAdmin(this.getEmployee().getEmployeeId());
    }
    
    public boolean isCRMStaff(){
        return employeeSessionBeanLocal.isCRMStaff(this.getEmployee().getEmployeeId());
    }
    
    public boolean isFRMStaff(){
        return employeeSessionBeanLocal.isFRMStaff(this.getEmployee().getEmployeeId());
    }    
    
    public boolean isMRPStaff(){
        return employeeSessionBeanLocal.isMRPStaff(this.getEmployee().getEmployeeId());
    }
    
    public boolean isSCMStaff(){
        return employeeSessionBeanLocal.isSCMStaff(this.getEmployee().getEmployeeId());
    }
    
    public boolean isAdminViewable(){
        return employeeSessionBeanLocal.isAdminViewable(this.getEmployee().getEmployeeId());
    }    
    public boolean isAdminCreatable(){
        return employeeSessionBeanLocal.isAdminCreatable(this.getEmployee().getEmployeeId());
    }
    public boolean isAdminEditable(){
        return employeeSessionBeanLocal.isAdminEditable(this.getEmployee().getEmployeeId());
    }
    public boolean isAdminDeletable(){
        return employeeSessionBeanLocal.isAdminDeletable(this.getEmployee().getEmployeeId());
    }
    public boolean isCRMViewable(){
        return employeeSessionBeanLocal.isCRMStaffViewable(this.getEmployee().getEmployeeId());
    }    
    public boolean isCRMCreatable(){
        return employeeSessionBeanLocal.isCRMStaffCreatable(this.getEmployee().getEmployeeId());
    }
    public boolean isCRMEditable(){
        return employeeSessionBeanLocal.isCRMStaffEditable(this.getEmployee().getEmployeeId());
    }
    public boolean isCRMDeletable(){
        return employeeSessionBeanLocal.isCRMStaffDeletable(this.getEmployee().getEmployeeId());
    }    
    public boolean isFRMViewable(){
        return employeeSessionBeanLocal.isFRMStaffViewable(this.getEmployee().getEmployeeId());
    }    
    public boolean isFRMCreatable(){
        return employeeSessionBeanLocal.isFRMStaffCreatable(this.getEmployee().getEmployeeId());
    }
    public boolean isFRMEditable(){
        return employeeSessionBeanLocal.isFRMStaffEditable(this.getEmployee().getEmployeeId());
    }
    public boolean isFRMDeletable(){
        return employeeSessionBeanLocal.isFRMStaffDeletable(this.getEmployee().getEmployeeId());
    }      
    public boolean isMRPViewable(){
        return employeeSessionBeanLocal.isMRPStaffViewable(this.getEmployee().getEmployeeId());
    }    
    public boolean isMRPCreatable(){
        return employeeSessionBeanLocal.isMRPStaffCreatable(this.getEmployee().getEmployeeId());
    }
    public boolean isMRPEditable(){
        return employeeSessionBeanLocal.isMRPStaffEditable(this.getEmployee().getEmployeeId());
    }
    public boolean isMRPDeletable(){
        return employeeSessionBeanLocal.isMRPStaffDeletable(this.getEmployee().getEmployeeId());
    }  
    public boolean isSCMViewable(){
        return employeeSessionBeanLocal.isSCMStaffViewable(this.getEmployee().getEmployeeId());
    }    
    public boolean isSCMCreatable(){
        return employeeSessionBeanLocal.isSCMStaffCreatable(this.getEmployee().getEmployeeId());
    }
    public boolean isSCMEditable(){
        return employeeSessionBeanLocal.isSCMStaffEditable(this.getEmployee().getEmployeeId());
    }
    public boolean isSCMDeletable(){
        return employeeSessionBeanLocal.isSCMStaffDeletable(this.getEmployee().getEmployeeId());
    }      
    
    public boolean isFirstLogin(){
        return employeeSessionBeanLocal.isFirstLogin(this.getEmployee().getEmployeeId());
    }
          
}
